// miniprogram/pages/community/community.js
var util = require('../../utils/util.js')
const app = getApp();
const db=wx.cloud.database().collection('user')
const dd=wx.cloud.database()
const db2=wx.cloud.database().collection('community')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentdate:'',
    main_top:'',
    choose:'',
    ischoose:false,
    search:'',
    issearch:false,
    isend:false,
    isrefresh:true,
    isrefresh1:true,
    commentinput:'',
    getprofile:'',
    inputcon:'',
    openid: '',
    id:"",
    c_index:"",
    array: ['二手闲置', '兼职实习', '校园资讯'],
    new_:'active',
    hot_:'',
    iscomment:false,
    good_src:'../../images/good.png',
    favor_src:'../../images/favor.png',
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    content:[],
    like:[],
    like_video:[],
    comment:[],
    collect:[],
    collect_video:[],
    mylike:[],
    mylike_video:[],
    mycollect:[],
    mycollect_video:[],
    video:[]
  },
  getUserProfile(e) {
    const that=this
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          
        })
        wx.setStorageSync('userInfo', that.data.userInfo)
        db.add({
          data:{
            _id:that.data.openid,
            nickName:that.data.userInfo.nickName,
            avatarUrl:that.data.userInfo.avatarUrl,
            city:that.data.userInfo.city,
            collect:[],
            like:[],
            collect_video:[],
              like_video:[],
          }
        }).then(()=>{
          that.setData({
            getprofile:true
          })
        })
      }
    })
  },
  inputchange:function(e){
    this.setData({
      inputcon: e.detail.value
    })
  },
  search:function(){
    var that = this;
    if(this.data.inputcon==''){
      wx.showToast({
        title: '请输入搜索内容!',
        icon:'none'
      })
    }else{
      wx.showLoading({
        title: '加载中...',
        mask:'true'
      })
      that.backtop()
      var key = this.data.inputcon;
      if(that.data.new_==''){
        dd.collection('video').orderBy("date","desc").where({
          type: dd.RegExp({
            regexp: key,
            options: 'i'
          })
      }).get({
          success: function(res) {
            console.log(res.data)
            that.setData({
              inputcon: '',
            })
            if(res.data.length==0){
              wx.showToast({
                title: '搜索内容暂无!',
                icon:'none'
              })
            }else{
              that.setData({
                video:res.data,
                issearch:true,
                search:key
              })
              that.getlike_video(that.data.mylike_video,res.data)
              that.getcollect_video(that.data.mycollect_video,res.data)
            }
           wx.hideLoading()
          }
        })
      }else{
        dd.collection('community').orderBy("date","desc").where({
          type: dd.RegExp({
            regexp: key,
            options: 'i'
          })
        })
        .get({
          success: function(res) {
            console.log(res.data)
            that.setData({
              inputcon: ''
            })
            if(res.data.length==0){
              wx.showToast({
                title: '搜索内容暂无!',
                icon:'none'
              })
            }else{
              that.setData({
                content:res.data,
                issearch:true,
                search:key,
                like:[],
                collect:[]
              })
              wx.setStorage({
                data: res.data,
                key: 'content',
              })
              that.getlike(that.data.mylike,res.data)
              that.getcollect(that.data.mycollect,res.data)
            }
            
          }
        })
      }
     
    }
  },
  formSubmit(e) {
    const that= this
    var input_content=e.detail.value.com_content
    var _time = util.formatTime(new Date())
    if(!input_content){
      wx.showToast({
        title: '请输入评论内容！',
        icon:'none'
      })
      return
    }
      wx.showLoading({
        title: '加载中...',
        mask:'true'
      })
      const _=dd.command
      if(that.data.new_==''){
        dd.collection('comment_video').add({
          data: {
            commentid: that.data.id,
            username:that.data.userInfo.nickName,
            img:that.data.userInfo.avatarUrl,
            content:input_content,
            date:_time
          },
          success: function(res) {
            // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
         /*    wx.removeStorageSync(that.data.id)
            wx.removeStorageSync('content') */
           
            that.close();
            console.log(that.data.video[that.data.c_index].comment)
            const a = `video[${that.data.c_index}].comment`
            var c=that.data.video[that.data.c_index].comment
              that.setData({
                [a]:++c,
                commentinput:''
              })
              dd.collection('video').doc(that.data.id).update({
                data:{
                  comment:c
                }
              }).then(()=>{
                
                wx.hideLoading()
                wx.showToast({
                  title: '评论成功！',
                })
              })
          }
        })
      }else{
        dd.collection('comment').add({
          data: {
            commentid: that.data.id,
            username:that.data.userInfo.nickName,
            img:that.data.userInfo.avatarUrl,
            content:input_content,
            date:_time
          },
          success: function(res) {
            // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
         /*    wx.removeStorageSync(that.data.id)
            wx.removeStorageSync('content') */
           
            that.close();
            console.log(that.data.content[that.data.c_index].comment)
            const a = `content[${that.data.c_index}].comment`
            var c=that.data.content[that.data.c_index].comment
              that.setData({
                [a]:++c,
                commentinput:''
              })
              db2.doc(that.data.id).update({
                data:{
                  comment:c
                }
              }).then(()=>{
                wx.setStorageSync('content', that.data.content)
                wx.hideLoading()
                wx.showToast({
                  title: '评论成功！',
                })
              })
          }
        })
      }
         
    
  },
  good_video:function(e){
    const that=this
    if(!that.data.hasUserInfo&&!that.data.getprofile){
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
          })
          wx.setStorageSync('userInfo', that.data.userInfo)
          db.add({
            data:{
              _id:that.data.openid,
              nickName:that.data.userInfo.nickName,
              avatarUrl:that.data.userInfo.avatarUrl,
              city:that.data.userInfo.city,
              collect:[],
              collect_video:[],
              like_video:[],
              like:[]
            }
          }).then(()=>{
            wx.showLoading({
              title: '加载中...',
              mask:'true'
            })
           
            var index=e.currentTarget.dataset.index
            var id=e.currentTarget.dataset.id
            const a = `like_video[${index}]`
            const b = `video[${index}].like_video`
            var c=that.data.video[index].like_video
            var d=that.data.mylike_video
           const _=dd.command
            if(that.data.like_video[index]==1){
              that.setData({
                [a]:0,
                [b]:--c
              })
              db.doc(that.data.openid).update({
                data:{
                  like_video:_.pull(id)
                }
              }).then(()=>{
                that.setData({
                  mylike_video:that.removeval(d,id)
                })
              })
              dd.collection('video').doc(id).update({
                data: {
                  // 表示指示数据库将字段自增 -1
                  like_video: _.inc(-1)
                },
                success: function(res) {
                  /* console.log(res.data) */
                  wx.setStorageSync('content', that.data.content)
            /*       wx.removeStorageSync('content') */
                  wx.hideLoading()
                }
              })
            }else{
              that.setData({
                [a]:1,
                [b]:++c
              })
              db.doc(that.data.openid).update({
                data:{
                  like_video:_.push(id)
                }
              }).then(()=>{
                that.setData({
                  mylike_video:d.concat(id)
                })
              })
              dd.collection('video').doc(id).update({
                data: {
                  // 表示指示数据库将字段自增 1
                  like_video: _.inc(1)
                },
                success: function(res) {
                  /* console.log(res.data) */
                  wx.setStorageSync('content', that.data.content)
                  wx.hideLoading()
                }
              })
            } 
          })
        }
      })
    }else{
      wx.showLoading({
        title: '加载中...',
        mask:'true'
      })
     
      var index=e.currentTarget.dataset.index
      var id=e.currentTarget.dataset.id
      const a = `like_video[${index}]`
      const b = `video[${index}].like_video`
      var c=that.data.video[index].like_video
      var d=that.data.mylike_video
     const _=dd.command
      if(that.data.like_video[index]==1){
        that.setData({
          [a]:0,
          [b]:--c
        })
        db.doc(that.data.openid).update({
          data:{
            like_video:_.pull(id)
          }
        }).then(()=>{
          that.setData({
            mylike_video:that.removeval(d,id)
          })
        })
        dd.collection('video').doc(id).update({
          data: {
            // 表示指示数据库将字段自增 -1
            like_video: _.inc(-1)
          },
          success: function(res) {
            /* console.log(res.data) */
      /*       wx.removeStorageSync('content') */
            wx.hideLoading()
          }
        })
      }else{
        that.setData({
          [a]:1,
          [b]:++c
        })
        db.doc(that.data.openid).update({
          data:{
            like_video:_.push(id)
          }
        }).then(()=>{
          that.setData({
            mylike_video:d.concat(id)
          })
        })
        dd.collection('video').doc(id).update({
          data: {
            // 表示指示数据库将字段自增 1
            like_video: _.inc(1)
          },
          success: function(res) {
            /* console.log(res.data) */
            wx.hideLoading()
          }
        })
      } 
    }
  },
  good:function(e){
    const that=this
    if(!that.data.hasUserInfo&&!that.data.getprofile){
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
          })
          wx.setStorageSync('userInfo', that.data.userInfo)
          db.add({
            data:{
              _id:that.data.openid,
              nickName:that.data.userInfo.nickName,
              avatarUrl:that.data.userInfo.avatarUrl,
              city:that.data.userInfo.city,
              collect:[],
              like:[],
              collect_video:[],
              like_video:[],
            }
          }).then(()=>{
            wx.showLoading({
              title: '加载中...',
              mask:'true'
            })
           
            var index=e.currentTarget.dataset.index
            var id=e.currentTarget.dataset.id
            const a = `like[${index}]`
            const b = `content[${index}].like`
            var c=that.data.content[index].like
            var d=that.data.mylike
           const _=dd.command
            if(that.data.like[index]==1){
              that.setData({
                [a]:0,
                [b]:--c
              })
              db.doc(that.data.openid).update({
                data:{
                  like:_.pull(id)
                }
              }).then(()=>{
                that.setData({
                  mylike:that.removeval(d,id)
                })
              })
              db2.doc(id).update({
                data: {
                  // 表示指示数据库将字段自增 -1
                  like: _.inc(-1)
                },
                success: function(res) {
                  /* console.log(res.data) */
                  wx.setStorageSync('content', that.data.content)
            /*       wx.removeStorageSync('content') */
                  wx.hideLoading()
                }
              })
            }else{
              that.setData({
                [a]:1,
                [b]:++c
              })
              db.doc(that.data.openid).update({
                data:{
                  like:_.push(id)
                }
              }).then(()=>{
                that.setData({
                  mylike:d.concat(id)
                })
              })
              db2.doc(id).update({
                data: {
                  // 表示指示数据库将字段自增 1
                  like: _.inc(1)
                },
                success: function(res) {
                  /* console.log(res.data) */
                  wx.setStorageSync('content', that.data.content)
                  wx.hideLoading()
                }
              })
            } 
          })
        }
      })
    }else{
      wx.showLoading({
        title: '加载中...',
        mask:'true'
      })
     
      var index=e.currentTarget.dataset.index
      var id=e.currentTarget.dataset.id
      const a = `like[${index}]`
      const b = `content[${index}].like`
      var c=that.data.content[index].like
      var d=that.data.mylike
     const _=dd.command
      if(that.data.like[index]==1){
        that.setData({
          [a]:0,
          [b]:--c
        })
        db.doc(that.data.openid).update({
          data:{
            like:_.pull(id)
          }
        }).then(()=>{
          that.setData({
            mylike:that.removeval(d,id)
          })
        })
        db2.doc(id).update({
          data: {
            // 表示指示数据库将字段自增 -1
            like: _.inc(-1)
          },
          success: function(res) {
            /* console.log(res.data) */
            wx.setStorageSync('content', that.data.content)
      /*       wx.removeStorageSync('content') */
            wx.hideLoading()
          }
        })
      }else{
        that.setData({
          [a]:1,
          [b]:++c
        })
        db.doc(that.data.openid).update({
          data:{
            like:_.push(id)
          }
        }).then(()=>{
          that.setData({
            mylike:d.concat(id)
          })
        })
        db2.doc(id).update({
          data: {
            // 表示指示数据库将字段自增 1
            like: _.inc(1)
          },
          success: function(res) {
            /* console.log(res.data) */
            wx.setStorageSync('content', that.data.content)
            wx.hideLoading()
          }
        })
      } 
    }

  },
  removeval:function(array,val){
    for (var i = 0; i < array.length; i++) {
      if (array[i] == val) {
        array.splice(i, 1);
      }
    }
    return array
  },
  favor_video:function(e){
    const that=this
    if(!that.data.hasUserInfo&&!that.data.getprofile){
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
          })
          wx.setStorageSync('userInfo', that.data.userInfo)
          db.add({
            data:{
              _id:that.data.openid,
              nickName:that.data.userInfo.nickName,
              avatarUrl:that.data.userInfo.avatarUrl,
              city:that.data.userInfo.city,
              collect:[],
              collect_video:[],
              like_video:[],
              like:[]
            }
          }).then(()=>{
            wx.showLoading({
              title: '加载中...',
              mask:'true'
            })
           
            var index=e.currentTarget.dataset.index
            var id=e.currentTarget.dataset.id
            const a = `collect_video[${index}]`
            const b = `video[${index}].collect_video`
            var c=that.data.video[index].collect_video
            var d=that.data.mycollect_video
           const _=dd.command
            if(that.data.collect_video[index]==1){
              that.setData({
                [a]:0,
                [b]:--c
              })
              db.doc(that.data.openid).update({
                data:{
                  collect_video:_.pull(id)
                }
              }).then(()=>{
                that.setData({
                  mycollect_video:that.removeval(d,id)
                })
              })
              dd.collection('video').doc(id).update({
                data: {
                  // 表示指示数据库将字段自增 -1
                  collect_video: _.inc(-1)
                },
                success: function(res) {
                  /* console.log(res.data) */
                  wx.setStorageSync('content', that.data.content)
            /*       wx.removeStorageSync('content') */
                  wx.hideLoading()
                }
              })
            }else{
              that.setData({
                [a]:1,
                [b]:++c
              })
              db.doc(that.data.openid).update({
                data:{
                  collect_video:_.push(id)
                }
              }).then(()=>{
                that.setData({
                  mycollect_video:d.concat(id)
                })
              })
              dd.collection('video').doc(id).update({
                data: {
                  // 表示指示数据库将字段自增 1
                  collect_video: _.inc(1)
                },
                success: function(res) {
                  /* console.log(res.data) */
                  wx.setStorageSync('content', that.data.content)
                  wx.hideLoading()
                }
              })
            } 
          })
        }
      })
    }else{
      wx.showLoading({
        title: '加载中...',
        mask:'true'
      })
     
      var index=e.currentTarget.dataset.index
      var id=e.currentTarget.dataset.id
      const a = `collect_video[${index}]`
      const b = `video[${index}].collect_video`
      var c=that.data.video[index].collect_video
      var d=that.data.mycollect_video
     const _=dd.command
      if(that.data.collect_video[index]==1){
        that.setData({
          [a]:0,
          [b]:--c
        })
        db.doc(that.data.openid).update({
          data:{
            collect_video:_.pull(id)
          }
        }).then(()=>{
          that.setData({
            mycollect_video:that.removeval(d,id)
          })
        })
        dd.collection('video').doc(id).update({
          data: {
            // 表示指示数据库将字段自增 -1
            collect_video: _.inc(-1)
          },
          success: function(res) {
            /* console.log(res.data) */
      /*       wx.removeStorageSync('content') */
            wx.hideLoading()
          }
        })
      }else{
        that.setData({
          [a]:1,
          [b]:++c
        })
        db.doc(that.data.openid).update({
          data:{
            collect_video:_.push(id)
          }
        }).then(()=>{
          that.setData({
            mycollect_video:d.concat(id)
          })
        })
        console.log(id)
        dd.collection('video').doc(id).update({
          data: {
            // 表示指示数据库将字段自增 1
            collect_video: _.inc(1)
          },
          success: function(res) {
            /* console.log(res.data) */
            wx.hideLoading()
          }
        })
      } 
    }
  },
  favor:function(e){
    const that=this
    if(!that.data.hasUserInfo&&!that.data.getprofile){
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
          })
          wx.setStorageSync('userInfo', that.data.userInfo)
          db.add({
            data:{
              _id:that.data.openid,
              nickName:that.data.userInfo.nickName,
              avatarUrl:that.data.userInfo.avatarUrl,
              city:that.data.userInfo.city,
              collect:[],
              like:[],
              collect_video:[],
              like_video:[],
            }
          }).then(()=>{
            wx.showLoading({
              title: '加载中...',
              mask:'true'
            })
            
            var id=e.currentTarget.dataset.id
            var index=e.currentTarget.dataset.index
            const a = `collect[${index}]`
            const b = `content[${index}].collect_num`
            var c=that.data.content[index].collect_num
            var d=that.data.mycollect
            const _=dd.command
            if(that.data.collect[index]==1){
              that.setData({
                [a]:0,
                [b]:--c
              })
              db.doc(that.data.openid).update({
                data:{
                  collect:_.pull(id)
                }
              }).then(()=>{
                that.setData({
                  mycollect:that.removeval(d,id)
                })
              })
              db2.doc(id).update({
                data: {
                  // 表示指示数据库将字段自增 -1
                  collect_num: _.inc(-1)
                },
                success: function(res) {
                  wx.setStorageSync('content', that.data.content)
                  wx.hideLoading()
                }
              })
            }else{
              that.setData({
                [a]:1,
                [b]:++c
              })
              db.doc(that.data.openid).update({
                data:{
                  collect:_.push(id)
                }
              }).then(()=>{
                that.setData({
                  mycollect:d.concat(id)
                })
              })
              db2.doc(id).update({
                data: {
                  collect_num: _.inc(1)
                },
                success: function(res) {
                  wx.setStorageSync('content', that.data.content)
                  wx.hideLoading()
                }
              })
            }
          })
        }
      })
    }else{
      wx.showLoading({
        title: '加载中...',
        mask:'true'
      })
      
      var id=e.currentTarget.dataset.id
      var index=e.currentTarget.dataset.index
      const a = `collect[${index}]`
      const b = `content[${index}].collect_num`
      var c=that.data.content[index].collect_num
      var d=that.data.mycollect
      const _=dd.command
      if(that.data.collect[index]==1){
        that.setData({
          [a]:0,
          [b]:--c
        })
        db.doc(that.data.openid).update({
          data:{
            collect:_.pull(id)
          }
        }).then(()=>{
          that.setData({
            mycollect:that.removeval(d,id)
          })
        })
        db2.doc(id).update({
          data: {
            // 表示指示数据库将字段自增 -1
            collect_num: _.inc(-1)
          },
          success: function(res) {
            wx.setStorageSync('content', that.data.content)
            wx.hideLoading()
          }
        })
      }else{
        that.setData({
          [a]:1,
          [b]:++c
        })
        db.doc(that.data.openid).update({
          data:{
            collect:_.push(id)
          }
        }).then(()=>{
          that.setData({
            mycollect:d.concat(id)
          })
        })
        db2.doc(id).update({
          data: {
            collect_num: _.inc(1)
          },
          success: function(res) {
            wx.setStorageSync('content', that.data.content)
            wx.hideLoading()
          }
        })
      }
    }

  },
  comment:function(e){
    const that=this
    
    that.setData({
      comment:[]
    })
    var index=e.currentTarget.dataset.index
    if(that.data.new_==''){
      dd.collection('comment_video').orderBy("date","desc").where({
        commentid:that.data.video[index]._id
      }).get({
       success: function(res) {
         // res.data 是包含以上定义的两条记录的数组
         wx.showLoading({
           title: '加载中',
           mask:'true'
         })
         that.setData({
           comment:res.data,
           id:that.data.video[index]._id,
           c_index:index
         }, () => {
           wx.hideLoading()
           })
       }
     })
    }else{
      dd.collection('comment').orderBy("date","desc").where({
        commentid:that.data.content[index]._id
      }).get({
       success: function(res) {
         // res.data 是包含以上定义的两条记录的数组
         wx.showLoading({
           title: '加载中',
           mask:'true'
         })
         that.setData({
           comment:res.data,
           id:that.data.content[index]._id,
           c_index:index
         }, () => {
           wx.hideLoading()
           })
       }
     })
    }
 
    that.setData({
      iscomment:true,
    })
  },
  close:function(){
    const that=this
    that.setData({
      iscomment:false,
    })
  },
  menu_new:function(){
    const that=this
    that.setData({
      new_:'active',
      hot_:''
    })
  },
  menu_hot:function(){
    const that=this
    var _time = util.formatTime(new Date())
    
    that.setData({
      new_:'',
      hot_:'active',
      like_video:[],
      collect_video:[],
      currentdate:_time
    })
    wx.showLoading({
      title: '加载中...',
      mask:'true'
    })
/*     var array=wx.getStorageSync('video')
    if(array){
      that.setData({
        video:array
      })
      wx.hideLoading()
      return
    } */
    dd.collection('video').get({
      success: function(res) {
        console.log(res.data)
        that.setData({
          video:res.data
        })
        if(that.data.mylike_video!=''){
          that.getlike_video(that.data.mylike_video,res.data)
        }
        if(that.data.mycollect_video!=''){
          that.getcollect_video(that.data.mycollect_video,res.data)
        }
        if(that.data.mylike_video==''&&that.data.mycollect_video==''){
          wx.hideLoading()
        }
       
      }
    })
  },
  menu_choose:function(){
    const that=this
  },
  publish:function(){
    const that=this
    if(that.data.hasUserInfo||that.data.getprofile){
      wx.navigateTo({
        url: '../choose/choose',
      })
    }else{
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          wx.setStorageSync('userInfo', that.data.userInfo)
         
          wx.navigateTo({
            url: '../choose/choose',
          })
          db.add({
            data:{
              _id:that.data.openid,
              nickName:that.data.userInfo.nickName,
              avatarUrl:that.data.userInfo.avatarUrl,
              city:that.data.userInfo.city,
              collect:[],
              like:[],
              collect_video:[],
              like_video:[],
            }
          }).then()
        }
      })
    }
    
  },
  detail:function(e){
    console.log(e.currentTarget.dataset.index)
    var index=e.currentTarget.dataset.index
    const that=this
    console.log(that.data.content[index])
    wx.navigateTo({
      url: '../detail/detail?content='+JSON.stringify(that.data.content[index]),
    })
  },
  getlike:function(like,content){
    const that=this
    if(that.data.like==''){
      wx.cloud.callFunction({
        // 云函数名称
        name: 'getlike',
        // 传给云函数的参数
        data: {
          like: like,
          content: content,
        },
        success: function(res) {
          console.log(res.result.array) 
          that.setData({
            like:res.result.array
          })
          if(that.data.like!=''&&that.data.collect!='')
          wx.hideLoading()
        },
        fail: console.error
      })
    }else{
      let old_like=that.data.like
      wx.cloud.callFunction({
        // 云函数名称
        name: 'getlike',
        // 传给云函数的参数
        data: {
          like: like,
          content: content,
        },
        success: function(res) {
          console.log(res.result.array) 
          that.setData({
            like:old_like.concat(res.result.array)
          })
          wx.hideLoading()
        },
        fail: console.error
      })
    }
    
    
  },
  getcollect:function(collect,content){
    const that=this
    if(that.data.collect==''){
      wx.cloud.callFunction({
        // 云函数名称
        name: 'getcollect',
        // 传给云函数的参数
        data: {
          collect: collect,
          content: content,
        },
        success: function(res) {
          console.log(res.result.array) 
          that.setData({
            collect:res.result.array
          })
          if(that.data.like!=''&&that.data.collect!='')
          wx.hideLoading()
        },
        fail: console.error
      })
    }else{
      let old_collect=that.data.collect
      wx.cloud.callFunction({
        // 云函数名称
        name: 'getcollect',
        // 传给云函数的参数
        data: {
          collect: collect,
          content: content,
        },
        success: function(res) {
          console.log(res.result.array) 
          that.setData({
            collect:old_collect.concat(res.result.array)
          })
          wx.hideLoading()
        },
        fail: console.error
      })
    }
  
  },
  getlike_video:function(like,content){
    const that=this
    if(that.data.like_video==''){
      wx.cloud.callFunction({
        // 云函数名称
        name: 'getlike',
        // 传给云函数的参数
        data: {
          like: like,
          content: content,
        },
        success: function(res) {
          console.log(res.result.array) 
          that.setData({
            like_video:res.result.array
          })
          wx.hideLoading()
        },
        fail: console.error
      })
    }else{
      let old_like=that.data.like_video
      wx.cloud.callFunction({
        // 云函数名称
        name: 'getlike',
        // 传给云函数的参数
        data: {
          like: like,
          content: content,
        },
        success: function(res) {
          console.log(res.result.array) 
          that.setData({
            like_video:old_like.concat(res.result.array)
          })
          wx.hideLoading()
        },
        fail: console.error
      })
    }
    
    
  },
  getcollect_video:function(collect,content){
    const that=this
    if(that.data.collect_video==''){
      wx.cloud.callFunction({
        // 云函数名称
        name: 'getcollect',
        // 传给云函数的参数
        data: {
          collect: collect,
          content: content,
        },
        success: function(res) {
          console.log(res.result.array) 
          that.setData({
            collect_video:res.result.array
          })
          wx.hideLoading()
        },
        fail: console.error
      })
    }else{
      let old_collect=that.data.collect_video
      wx.cloud.callFunction({
        // 云函数名称
        name: 'getcollect',
        // 传给云函数的参数
        data: {
          collect: collect,
          content: content,
        },
        success: function(res) {
          console.log(res.result.array) 
          that.setData({
            collect_video:old_collect.concat(res.result.array)
          })
          wx.hideLoading()
        },
        fail: console.error
      })
    }
  
  },
  onLoad: function (options) {
    const that=this
    wx.clearStorage()
    app.getOpenId.then(res => {
      that.setData({
        openid:res
      })
     wx.setStorage({
      data: res,
      key: 'openid',
    })
      db.doc(res).get({
          success: function(res) {
            if(res.data){
              that.setData({
                getprofile:true,
                userInfo:{
                  nickName:res.data.nickName,
                  avatarUrl:res.data.avatarUrl
                },
                mylike:res.data.like,
                mycollect:res.data.collect,
                mylike_video:res.data.like_video,
                mycollect_video:res.data.collect_video
              },()=>{
                wx.setStorage({
                  data: that.data.userInfo,
                  key: 'userInfo',
                })
                that.getlike(res.data.like,that.data.content)
                that.getcollect(res.data.collect,that.data.content)
              })
            }
          },
      })
    })
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  bindPickerChange:function(e){
    const that=this
    that.backtop()
    wx.showLoading({
      title: '加载中...',
      mask:'true'
    })
    if(that.data.new_==''){
      dd.collection('video').where({
        type:that.data.array[e.detail.value]
    }).get({
        success: function(res) {
          console.log(res.data)
          that.setData({
            video:res.data,
            choose:e.detail.value,
            ischoose:true,
            issearch:false,
            
          })
          that.getlike_video(that.data.mylike_video,res.data)
          that.getcollect_video(that.data.mycollect_video,res.data)
          wx.hideLoading()
        }
      })
    }else{
      db2.orderBy("date","desc").where({
        type:that.data.array[e.detail.value]
    }).get({
      success: function(res) {
        console.log(res)
        that.setData({
          choose:e.detail.value,
          content:res.data,
          ischoose:true,
          issearch:false,
          like:[],
            collect:[]
        })
        wx.setStorage({
          data: res.data,
          key: 'content',
        })
        that.getlike(that.data.mylike,res.data)
        that.getcollect(that.data.mycollect,res.data)

      }
    })
    }
  },
  
  onShow: function () {
    
    const that=this
    var _time = util.formatTime(new Date())
    that.setData({
      currentdate:_time
    })
    wx.showLoading({
      title: '加载中...',
      mask:'true'
    })
    var array=wx.getStorageSync('content')
    if(array){
      that.setData({
        content:array,
        like:[],
        collect:[]
      },()=>{
        if(that.data.mylike!=''){
          that.getlike(that.data.mylike,that.data.content)
        }
        if(that.data.mycollect!=''){
          that.getcollect(that.data.mycollect,that.data.content)
        }

        if(that.data.mylike==''&&that.data.mycollect==''){
            wx.hideLoading({
                  success: (res) => {},
                })
        }
      })
   
      return
    }
    db2.orderBy("date","desc").get({
      
      success: function(res) {
        that.setData({
          content:res.data,
          like:[],
          collect:[]
        },()=>{
          if(that.data.mylike!=''){
            that.getlike(that.data.mylike,that.data.content)
          }
          if(that.data.mycollect!=''){
            that.getcollect(that.data.mycollect,that.data.content)
          }
          if(that.data.mylike==''&&that.data.mycollect==''){
            wx.hideLoading({
                  success: (res) => {},
                })
        }
        })
        wx.setStorage({
          key:'content',
          data:res.data
        })
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  refresh:function(){
    /*下拉刷新 */
    wx.removeStorageSync('content')
    this.onShow()
    this.setData({
      isrefresh:false,
      issearch:false,
      ischoose:false
    })
    console.log(this.data.isrefresh)

  },
  refresh1:function(){
    this.menu_hot()
    this.setData({
      isrefresh1:false
    })
  },
  lower(e) {
    const that=this
/*     if(that.data.isend){
      return
    } */
    wx.showLoading({
      title: '加载中...',
      mask:'true'
    })
   console.log(that.data.issearch)
    //判断是搜索的还是筛选的
    if(that.data.issearch==true){
      var key = that.data.search;
      if(that.data.new_==''){
        var x=that.data.video.length
        let old_data = that.data.video
        var _=dd.command
        dd.collection('video').orderBy("date","desc").where({
          type: dd.RegExp({
            regexp: key,
            options: 'i'
          }),
          date:_.lt(that.data.currentdate)
      }).skip(x).get({
          success: function(res) {
            if(res.data.length==0){
              wx.hideLoading()
              wx.showToast({
                title: '已经到底啦!',
                icon:'none'
              })
            }else{
              this.setData({
                video: old_data.concat(res.data),
              })
              that.getlike_video(that.data.mylike_video,res.data)
              that.getcollect_video(that.data.mycollect_video,res.data)
              wx.hideLoading()
            }
          
          }
        })
      }else{//在论坛页
        var x=that.data.content.length
        let old_data = that.data.content
        var _=dd.command
        dd.collection('community').orderBy("date","desc").where({
          type: dd.RegExp({
            regexp: key,
            options: 'i'
          }),
          date:_.lt(that.data.currentdate)
        }).skip(x)
        .get({
          success: function(res) {
            
            if(res.data.length==0){
              wx.hideLoading()
              wx.showToast({
                title: '已经到底啦!',
                icon:'none'
              })
            }else{
              that.setData({
                content: old_data.concat(res.data),
              })
              that.getlike(that.data.mylike,res.data)
              that.getcollect(that.data.mycollect,res.data)
              wx.hideLoading()
            }
          }
        })
      }
    }else if(that.data.ischoose==true){
      console.log('我在筛选')
      if(that.data.new_==''){
        var x=that.data.video.length
        let old_data = that.data.video
        var _=dd.command
        dd.collection('video').orderBy("date","desc").where({
          type: that.data.choose,
          date:_.lt(that.data.currentdate)
      }).skip(x).get({
          success: function(res) {
            if(res.data.length==0){
              wx.hideLoading()
              wx.showToast({
                title: '已经到底啦!',
                icon:'none'
              })
            }else{
              this.setData({
                video: old_data.concat(res.data),
              })
              that.getlike_video(that.data.mylike_video,res.data)
              that.getcollect_video(that.data.mycollect_video,res.data)
              wx.hideLoading()
            }
          
          }
        })
      }else{//在论坛页
        var x=that.data.content.length
        let old_data = that.data.content
        var _=dd.command
        dd.collection('community').orderBy("date","desc").where({
          type: that.data.choose,
          date:_.lt(that.data.currentdate)
        }).skip(x)
        .get({
          success: function(res) {
            if(res.data.length==0){
              wx.hideLoading()
              wx.showToast({
                title: '已经到底啦!',
                icon:'none'
              })
            }else{
              that.setData({
                content: old_data.concat(res.data),
              })
              that.getlike(that.data.mylike,res.data)
              that.getcollect(that.data.mycollect,res.data)
              wx.hideLoading()
            }
          }
        })
      }
      }
    else {
      if(that.data.new_==''){
        var x=that.data.video.length
        let old_data = that.data.video
        var _=dd.command
        dd.collection('video').orderBy("date","desc").where({
         
          date:_.lt(that.data.currentdate)
      }).skip(x).get({
          success: function(res) {
            if(res.data.length==0){
              wx.hideLoading()
              wx.showToast({
                title: '已经到底啦!',
                icon:'none'
              })
            }else{
              this.setData({
                video: old_data.concat(res.data),
              })
              that.getlike_video(that.data.mylike_video,res.data)
              that.getcollect_video(that.data.mycollect_video,res.data)
              wx.hideLoading()
            }
          
          }
        })
      }else{//在论坛页
        var x=that.data.content.length
        let old_data = that.data.content
        var _=dd.command
        dd.collection('community').orderBy("date","desc").where({
         
          date:_.lt(that.data.currentdate)
        }).skip(x)
        .get({
          success: function(res) {
            if(res.data.length==0){
              wx.hideLoading()
              wx.showToast({
                title: '已经到底啦!',
                icon:'none'
              })
            }else{
              that.setData({
                content: old_data.concat(res.data),
              })
              that.getlike(that.data.mylike,res.data)
              that.getcollect(that.data.mycollect,res.data)
              wx.hideLoading()
            }
          }
        })
      }
    }
  },
  lower2(e){
    const that=this
    wx.showLoading({
      title: '加载中...',
      mask:'true'
    })
    var x=that.data.comment.length
        let old_data = that.data.comment
        var _=dd.command
    if(that.data.new_==''){
      
      dd.collection('comment_video').orderBy('date','desc').where({
        commentid:that.data.id,
        date:_.lt(that.data.currentdate)
      }).skip(x) // 限制返回数量为 20 条
      .get()
      .then(res => {
        console.log(res.data)
      // 利用concat函数连接新数据与旧数据
     if(res.data.length==0){
      wx.hideLoading()
       wx.showToast({
         title: '已经到底了！',
         icon:'none'
       })
       return
     }
        this.setData({
          comment: old_data.concat(res.data),
        })
        console.log(res.data)
        wx.hideLoading()
      })
      .catch(err => {
        console.error(err)
      })
    }else{
      dd.collection('comment').orderBy('date','desc').where({
        commentid:that.data.id,
        date:_.lt(that.data.currentdate)
      }).skip(x) // 限制返回数量为 20 条
      .get()
      .then(res => {
        console.log(res.data)
      // 利用concat函数连接新数据与旧数据
     if(res.data.length==0){
      wx.hideLoading()
       wx.showToast({
         title: '已经到底了！',
         icon:'none'
       })
       return
     }
        this.setData({
          comment: old_data.concat(res.data),
        })
        console.log(res.data)
        wx.hideLoading()
      })
      .catch(err => {
        console.error(err)
      })
    }
       
     
  },
  backtop:function(){
    this.setData({
      main_top:0
    })
  }


})